package com.meizu.asum.richtextsdk.table;

import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Description： <br>
 * User：Xie JunWen <br>
 * Email：v-xiejunwen@meizu.com <br>
 * CreateTime：2018/1/15  10:38 <br>
 * Remark：无
 */
public abstract class BaseEmoticonIconTable {
    public static Map<String, Integer> emoticonNameResIdMap;
    public static Map<Integer, String> emotionResIdNameMap;
    public static ArrayList<String> emoticonIconNames;
    public static ArrayList<Integer> emoticonIconResIds;
    public int deleteResId;
    public int[] pageConfigs;

    public void initialize(boolean isReload) {
        if (emoticonNameResIdMap == null || isReload) {
            ArrayList<String> newEmoticonIconNames = getEmoticonIconNames();
            ArrayList<Integer> newEmoticonIconResIds = getEmoticonIconResIds();

            if (newEmoticonIconNames == null || newEmoticonIconResIds == null || newEmoticonIconNames.size() != newEmoticonIconResIds.size()) {
                Log.e("XJW", "BaseEmoticonIconTable : emoticon icons initialize error：emoticonIconNames is null -->" + (newEmoticonIconNames == null) + ",emoticonIconResIds is null -->" + (newEmoticonIconResIds == null));
                return;
            }

            if (emoticonNameResIdMap != null) {
                emoticonNameResIdMap.clear();
            }
            emoticonNameResIdMap = new HashMap<>();

            if (emotionResIdNameMap != null) {
                emotionResIdNameMap.clear();
            }
            emotionResIdNameMap = new HashMap<>();

            if (emoticonIconNames != null) {
                emoticonIconNames.clear();
            }
            emoticonIconNames = newEmoticonIconNames;

            if (emoticonIconResIds != null) {
                emoticonIconResIds.clear();
            }
            emoticonIconResIds = newEmoticonIconResIds;

            for (int i = 0; i < emoticonIconNames.size(); i++) {
                emoticonNameResIdMap.put(emoticonIconNames.get(i), emoticonIconResIds.get(i));
                emotionResIdNameMap.put(emoticonIconResIds.get(i), emoticonIconNames.get(i));
            }

            deleteResId = getDeleteResId();
            pageConfigs = getPageConfigs();
        }
    }

    public ArrayList<Integer> getPageResIdsFromPage(int pageIndex) {
        ArrayList<Integer> pageResIds = new ArrayList<>();
        if (emoticonIconResIds == null) {
            Log.e("XJW", "BaseEmoticonIconTable : please initialize first");
            return pageResIds;
        }

        if (pageConfigs == null) {
            Log.e("XJW", "BaseEmoticonIconTable : page config is not initialize,please finish method getPageConfigs");
            return pageResIds;
        }

        if (pageIndex > pageConfigs.length) {
            Log.e("XJW", "BaseEmoticonIconTable : page index is " + pageIndex + ",but page configs length is " + pageConfigs.length);
            return pageResIds;
        }

        if (pageIndex < 0) {
            Log.e("XJW", "BaseEmoticonIconTable : page index must longer than -1,now the page index is " + pageIndex);
            return pageResIds;
        }

        int start = 0;
        int end = 0;
        for (int i = 0; i < pageConfigs.length; i++) {
            if (pageIndex == i) {
                end = start + pageConfigs[i];
                break;
            } else {
                start += pageConfigs[i];
            }
        }
        Log.i("XJW", "Start：" + start + "，End：" + end + "：" + emoticonIconResIds.size());

        for (int i = start; i < end; i++) {
            if (emoticonIconResIds.size() > i) {
                pageResIds.add(emoticonIconResIds.get(i));
            } else {
                pageResIds.add(-1);
            }
        }
        pageResIds.add(deleteResId);

        return pageResIds;
    }

    public String getEmotionIconNameFromResId(int resId) {
        String name = "";
        if (emotionResIdNameMap == null) {
            Log.e("XJW", "BaseEmoticonIconTable : please initialize first");
        } else {
            if (emotionResIdNameMap.containsKey(resId)) {
                name = emotionResIdNameMap.get(resId);
            }
        }

        return name;
    }

    public int getEmotionResIdFromName(String name) {
        int resId = -1;
        if (emoticonNameResIdMap == null) {
            Log.e("XJW", "BaseEmoticonIconTable : please initialize first");
        } else {
            if (emoticonNameResIdMap.containsKey(name)) {
                resId = emoticonNameResIdMap.get(name);
            }
        }

        return resId;
    }

    /**
     * 设置表情包的名字集合
     *
     * @return
     */
    public abstract ArrayList<String> getEmoticonIconNames();

    /**
     * 设置表情包的资源ID集合
     *
     * @return
     */
    public abstract ArrayList<Integer> getEmoticonIconResIds();

    /**
     * 设置删除键资源ID
     *
     * @return
     */
    public abstract int getDeleteResId();

    /**
     * 设置每一页表情的数量，自动补全删除键资源ID
     *
     * @return
     */
    public abstract int[] getPageConfigs();
}
